<?php
/**
 * 栏目管理
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ContypeController.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Admin\Controller;
class ContypeController extends CommonController{
    private $model;
    private $service;
    /**
     * 初始化类
     */
    public function _initialize() {
        parent::_initialize();
        $this->model = D("Contype");
        $this->service = D("Contype", 'Service');
    }
    
    /**
     * 栏目管理
     */
    public function index() {
        //$list = $this->service->getAll("id, reid, topid, sortrank, typename, model_id, ispart, ishidden");
        $list = $this->service->getTypes();
        //$list = $this->getPagination("Contype");
        /*if(isArrayAndNotEmpty($this->language)){
            $tmp = array();
            foreach ($this->language as $key => $lang) {
                foreach ($list as $cat) {
                    if($cat["lang"] == $key){
                        $tmp[$key]["title"] = $lang;
                        $tmp[$key]["cat"][] = $cat;
                    }
                }
            }
            $list = $tmp;
            $this->assign("HOME_LANG_SWITCH_ON", C("HOME_LANG_SWITCH_ON"));
        }*/
        //echo "<pre>";print_r($tmp);die;
        $this->assign("list", $list);
        $this->display();
    }
    
    /**
     * 添加栏目
     */
    public function add() {
        $id = I("get.id", 0, "intval");
        if($id > 0){
            $myrow = $this->service->getById($id);
            $data['reid'] = $id;
            $data['topid'] = $myrow['topid'];
            $data['model_id'] = $myrow['model_id'];
            $data['typedir'] = $myrow['typedir'];
        }else{
            $data['reid'] = 0;
            $data['topid'] = 0;
            $data['model_id'] = 1;
            $data['typedir'] = "";
        }
        $models = D('Model', 'Service')->getAll("*", "", array("status" => 1));
        $types = $this->service->getTypes();
        $this->assign('types', $types);
        $this->assign('models', $models);
        $this->assign($data);
        $this->display();
    }
    
    /**
     * 创建栏目
     * @return type
     */
    public function create() {
        $types = I("post.type");
        if(!isArrayAndNotEmpty($types)){
            return $this->errorReturn("无效操作");
        }
        if($types['topid'] == 0 && $types['reid'] > 0){
            $types['topid'] = $types['reid'];
        }
        /*if($types['ispart'] != 2){
            $referpath = I("post.referpath");
            $nextdir = I("post.nextdir");
            $upinyin = I("post.upinyin", 0, 'intval');
            //栏目的参照目录
            if($referpath=='cmspath') $nextdir = '{cmspath}';
            if($referpath=='basepath') $nextdir = '';

            //用拼音命名
            if($upinyin==1 || $types['typedir'] == '')
            {
                $typedir = GetPinyin(stripslashes($types['typename']));
            }
            $typedir = $nextdir.'/'.$typedir;
            $types['typedir'] = preg_replace("#\/{1,}#", "/", $typedir);
        }*/
        //创建目录
        /*if($typs['ispart'] != 2)
        {
            $true_typedir = str_replace("{cmspath}", YDCMS_ROOT, $typs['typedir']);
            $true_typedir = preg_replace("#\/{1,}#", "/", $true_typedir);
            if(!CreateDir($true_typedir))
            {
                return $this->errorReturn("创建目录 {$true_typedir} 失败，请检查你的路径是否存在问题！");
                exit();
            }
        }*/
        $result = $this->service->add($types);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('添加栏目成功！', U('index'));
    }
    
    /**
     * 编辑
     */
    public function edit() {
        $id = I("get.id", 0, "intval");
        if($id > 0){
            $myrow = $this->service->getById($id);
        }else{
            $this->error("无效操作！");
        }
        $models = D('Model', 'Service')->getAll("*", "", array("status" => 1));
        $types = $this->service->getTypes();
        $this->assign('types', $types);
        $this->assign('models', $models);
        $this->assign($myrow);
        $this->display();
    }
    
    /**
     * 更新栏目
     */
    public function update() {
        $types = I("post.type");
        if(!isArrayAndNotEmpty($types)){
            return $this->errorReturn("无效操作");
        }
        $result = $this->service->update($types);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('更新栏目成功！', U('index'));
    }
    
    /**
     * 更新所有排序
     * @return type
     */
    public function upRankAll() {
        $types = I("post.type");
        if(!isArrayAndNotEmpty($types)){
            return $this->errorReturn("无效操作");
        }
        $result = $this->service->upRankAll($types);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('更新排序成功！', U('index'));
    }
    
    /**
     * 删除
     * @return type
     */
    public function delete() {
        $id = I("post.id", 0, 'intval');
        if($id <= 0){
            return $this->errorReturn("无效操作");
        }
        $result = $this->service->delete($id);
        if(!$result['status']){
            return $this->errorReturn($result['data']['error']);
        }
        return $this->successReturn('删除栏目成功！', U('index'));
    }
    
    /**
     * 批量添加栏目
     */
    public function quick() {
        
        $data['reid'] = 0;
        $data['topid'] = 0;
        $data['model_id'] = 1;
        $data['typedir'] = "";

        $models = D('Model', 'Service')->getAll("*", "", array("status" => 1));
        $types = $this->service->getTypes();
        $this->assign('types', $types);
        $this->assign('models', $models);
        $this->assign($data);
        $this->display();
    }
    
    /**
     * 批量保存栏目
     */
    public function savequick() {
        $savetype = I("post.savetype", "");
        $public = I("post.public");
        $referpath = I("post.referpath");
        $nextdir = I("post.nextdir");
        if(!isArrayAndNotEmpty($public)){
            $this->errorReturn("无效操作！");
        }
        $model_info = D("Model", "Service")->getById($public['model_id'], FALSE);
        if(!isArrayAndNotEmpty($model_info)){
            $this->errorReturn("无效操作！");
        }
        $public['isdefault'] = isset($public['isdefault'])? intval($public['isdefault']) : -1;
        $public['tempindex'] = "index_{$model_info['nid']}";
        $public['templist'] = "list_{$model_info['nid']}";
        $public['temparticle'] = "article_{$model_info['nid']}";
        if($savetype){
            $toptype = $this->service->getById($public['reid']);
            if(!isArrayAndNotEmpty($toptype)){
                $this->error("选择上级栏目有误！");
            }
            $public['lang'] = $toptype['lang'];
            $public['model_id'] = $toptype['model_id'];
            $sortrank = I("post.relrank");
            $reltype = I("post.reltype");
            
            for($i = 0; $i < count($reltype); $i++){
                $data = array();
                $data['typename'] = $reltype[$i];
                if(empty($data['typename'])){
                    continue;
                }
                $toptypedir = GetPinyin(stripslashes($data['typename']));
                switch ($referpath) {
                    case 'parent':
                        $toptypedir = $nextdir.'/'.$toptypedir;
                        break;
                    case 'typepath':
                        $toptypedir = isset($toptype['typedir'])? $reltype['typedir'].'/'.$toptypedir : '/'.$toptypedir;
                        break; 
                    default:
                        $toptypedir = '/'.$toptypedir;
                        break;
                }
                $data['typedir'] = $toptypedir;
                $data['sortrank'] = $sortrank[$i];
                $data = array_merge($data, $public);
                $data['topid'] = $toptype['id'];
                $data['reid'] = $toptype['id'];
                
                if(false === ($type = $this->model->create($data))){
                    continue;
                }
                if(false === $this->model->add($type)){
                    continue;
                }
            }
        }else{
            $sortrank = I("post.rank");
            $toptype = I("post.toptype");
            $sontype = I("post.sontype");
            
            for($i = 0; $i < count($toptype); $i++){
                $data = array();
                $data['typename'] = $toptype[$i];
                if(empty($data['typename'])){
                    continue;
                }
                $toptypedir = GetPinyin(stripslashes($data['typename']));
                $data['typedir'] = $toptypedir = $referpath=='parent' ? $nextdir.'/'.$toptypedir : '/'.$toptypedir;
                $data['sortrank'] = $sortrank[$i];
                $data = array_merge($data, $public);
                if(false === ($type = $this->model->create($data))){
                    continue;
                }
                if(false === $this->model->add($type)){
                    continue;
                }
                $tid = $this->model->getLastInsID();
                if($tid && $sontype[$i]){
                    $sontypes = explode(',',$sontype[$i]);
                    foreach($sontypes as $k=>$v){
                        $son_data = array();
                        $v = trim($v);
                        if($v=='')
                        {
                            continue;
                        }
                        $son_data['typedir'] = $toptypedir.'/'.GetPinyin(stripslashes($v));
                        $son_data['typename'] = $v;
                        $son_data['sortrank'] = $k;
                        $son_data = array_merge($son_data, $public);
                        $son_data['topid'] = $tid;
                        $son_data['reid'] = $tid;
                        if(false === ($stype = $this->model->create($son_data))){
                            continue;
                        }
                        if(false === $this->model->add($stype)){
                            continue;
                        }
                    }
                }
            }
        }
        return $this->successReturn("成功添加指定栏目！", U("index"));
    }
}
